﻿--[[
   @brief    rail_dlc_manager view
   @author   lieberli
   @version  1.0
   @date     2017/05/29
--]]

rail_dlc_manager_view = {
    pDialog = nil;
    dwRootId = 0;

    show = function()
        ierd_tgp.tpf_ui.Log("rail_dlc_manager_view show.");
        rail_dlc_manager_view.pDialog = ui_utility.get_ctrl_ptr_by_id(rail_dlc_manager_view.dwRootId);

        if rail_dlc_manager_view.pDialog == nil then
            rail_dlc_manager_view.dwRootId = layout_mgr.restore_layout("rail_dlc_mgr_dlg.tpf");
            if rail_dlc_manager_view.dwRootId ~= 0 then
                --shadow_helper.create_shadow(rail_dlc_manager_view.dwRootId);
                rail_dlc_manager_view.pDialog = ui_utility.get_ctrl_ptr_by_id(rail_dlc_manager_view.dwRootId);
            end
        end

        if rail_dlc_manager_view.pDialog ~= nil then
            if rail_dlc_manager_view.pDialog:IsVisible() == TRUE then --打开后再次打开
                rail_dlc_manager_view.pDialog:BringToTop();
            else    --关闭后打开
                rail_dlc_manager_view.pDialog:Show(TRUE);
                rail_dlc_manager_view.pDialog:BringToTop();
                rail_dlc_manager_view.load_html();
            end
        end
    end;

    load_html = function()
        local pQBWeb = ui_utility.get_ctrl_ptr_by_path(rail_dlc_manager_view.dwRootId, "QBWebBrowser_DLCMgr");
        if pQBWeb == nil then
            return
        end
        local url = LoadStr("DLC_MANAGER_URL");
        local login_url = web_utility.make_login_status_url(url, login_model.main_uin);
        local const_ui_str, str_holder = U8toCUIStr(login_url);
        pQBWeb:Navigate(const_ui_str);
    end;

    close = function(uilib, dwClickId)
        if rail_dlc_manager_view.pDialog ~= nil then
            --shadow_helper.destroy_shadow(rail_dlc_manager_view.dwRootId);
            rail_dlc_manager_view.pDialog:Show(FALSE);
            layout_mgr.delay_remove_ctrl(rail_dlc_manager_view.dwRootId);
            rail_dlc_manager_view.dwRootId = 0;
            rail_dlc_manager_view.pDialog = nil;
        end;
    end;

    on_js_callback = function(uilib, fuc_name, json)
        if fuc_name == "jc_get_current_module" then
            local rsp = {game_id = plugin_tool_simple_panel.cur_game_id};
            local json_str = base_utility.table_to_json(rsp);
            local const_func_name, str_func_name = U8toCUIStr("on_current_module");
            local const_json_param, str_json_param = U8toCUIStr(json_str);
            local pQBWeb = ui_utility.get_ctrl_ptr_by_path(rail_dlc_manager_view.dwRootId, "QBWebBrowser_DLCMgr");
            if pQBWeb ~= nil then
                pQBWeb:ExcecuteJS(const_func_name, const_json_param);
            end
        elseif fuc_name == "jc_jump_to_detail" then
            local param_tbl = base_utility.json_to_table(json);
            local url = LoadStr("WEGAME_DETAIL_URL") .. param_tbl.game_id;
            local info = {};
            info.type = "jump_url";
            info.id = 50000013;
            info.url = url;
            web_utility.open_link(base_utility.table_to_json(info));
            rail_dlc_manager_view.close();
        end
    end;
};
